//
//  CommentsView.m
//  Azkar
//
//  Created by 王易 on 2018/8/27.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "CommentsView.h"
#import "SpeedyCell.h"

/**
 评论View
 @author yzl
 */
@interface CommentsView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,weak) UITableView *tableView;
@property (nonatomic,weak) UIButton *send;
@property (nonatomic,strong) SpeedyModel *speedyModel;
@property (nonatomic,weak) UIButton *message;
@end
@implementation CommentsView

- (void)initContentUI
{
    self.myHorzMargin = 0;
    self.myBottom = 0;
    self.wrapContentHeight = YES;
    self.backgroundColor = Color(@"#FFFFFF");
    
    MyRelativeLayout *contentLayout = [MyRelativeLayout new];
    contentLayout.myHorzMargin = 0;
    contentLayout.myTop = 0;
    contentLayout.myHeight = 50;
    [self addSubview:contentLayout];
    UIButton *send = [UIButton buttonWithType:UIButtonTypeCustom];
    self.send = send;
    [send changeTitle:TLString(@"social_send")];
    [send changeTitleColor:Color(@"#00732F")];
    send.titleLabel.font = FontSize(15);
    send.myTrailing = 12;
    send.myVertMargin = 0;
    send.wrapContentWidth = YES;
    [contentLayout addSubview:send];
    WeakSelf
    [send setHyb_onTouchUp:^(UIButton *sender) {
        StrongSelf
          [self.input endEditing:YES];
        
            [self hideTools];
        if (self.input.text.length == 0) {
            [AlertTools showMessage:TLString(@"greeting_cards_say_something")];
            return ;
        }
      
        if (self.sendBLock) {
            self.sendBLock(self.input);
        }
    }];
    
    MyRelativeLayout *inputLayout = [MyRelativeLayout new];
    inputLayout.myLeading = 12;
    inputLayout.trailingPos.equalTo(send.leadingPos).offset(15);
    inputLayout.myHeight = 34;
    inputLayout.myCenterY = 0;
    inputLayout.backgroundColor = Color(@"#EFEFEF");
    inputLayout.layer.cornerRadius = 17;
    inputLayout.layer.masksToBounds = YES;
    [contentLayout addSubview:inputLayout];
    
    UIButton *messageImage = [UIButton buttonWithType:UIButtonTypeCustom];
    self.message = messageImage;
    [messageImage changeImage:Image(@"quick_comments_btn")];
    messageImage.myTrailing = 4;
    messageImage.myCenterY = 0;
    messageImage.myWidth = 30;
    messageImage.myHeight = 30;
    [inputLayout addSubview:messageImage];
    
    [messageImage setHyb_onTouchUp:^(UIButton *sender) {
        StrongSelf
        [self.input endEditing:YES];
        [self loadData];
    }];
    
    UITextField *input = [UITextField new];
    self.input = input;
    input.myLeading = 18;
    input.trailingPos.equalTo(messageImage.leadingPos).offset(10);
    input.myHeight = 30;
    input.myCenterY = 0;
    input.placeholder = TLString(@"greeting_cards_say_something");
    input.textColor = Color(@"#525252");
    input.font = FontSize(14);
    [inputLayout addSubview:input];
    
    
    UITableView *tableView = [UITableView new];
    self.tableView = tableView;
    tableView.topPos.equalTo(contentLayout.bottomPos);
    tableView.myHeight = 270;
    tableView.myHorzMargin = 0;
    tableView.myVisibility = MyVisibility_Gone;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.estimatedRowHeight = 44;
    tableView.estimatedSectionFooterHeight = 0;
    tableView.estimatedSectionHeaderHeight = 0;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [SpeedyCell registerNibToTable:tableView];
    [self addSubview:tableView];
    
    UIImageView *emptyView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"no_content_img"]];
    [self.tableView addSubview:emptyView];
    emptyView.centerX = kScreenWidth/2;
    emptyView.centerY = 270/2;
    emptyView.tag = 1024;
    [tableView addSubview:emptyView];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chatKeyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.speedyModel.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SpeedyCell *cell = [tableView dequeueReusableCellWithIdentifier:[SpeedyCell reuseIdentifier]];
    cell.listModel = self.speedyModel.data[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SpeedyListModel  *listModel = self.speedyModel.data[indexPath.row];
    self.input.text = listModel.reply;
}
- (void)loadData
{
//    if (!self.lang_type) {
//        [AlertTools showMessage:@"数据异常"];
//        return;
//    }
    
    [AlertTools showLoading];
    
    NSString *url = [NSString stringWithFormat:@"%@/%@/default_replay",BlessBlog,self.lang_type];
    WeakSelf
    [[AzkarApi getData:url withParm:nil]subscribeNext:^(id x) {
        StrongSelf
        [AlertTools hideLoading];
        
        if (x) {
            NSDictionary *data = @{@"data":x};
            SpeedyModel *model = [SpeedyModel yy_modelWithDictionary:data];
            self.speedyModel = model;
        }
        [self.tableView viewWithTag:1024].hidden = self.speedyModel.data.count;
        
        self.message.selected = !self.message.selected;
        self.tableView.myVisibility = self.message.selected ? MyVisibility_Visible : MyVisibility_Gone;
        self.myBottom = 0;
        [self layoutAnimationWithDuration:0.5];
        [self.tableView reloadData];
    } error:^(NSError *error) {
//         [AlertTools hideLoading];
    }];
}
- (void)hideTools
{
   
    self.tableView.myVisibility =  MyVisibility_Gone;
    self.myBottom = 0;
    [self layoutAnimationWithDuration:0.5];
}
- (void)chatKeyboardWillChangeFrame:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect beginFrame = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    
    void(^animations)(void) = ^{
        [self willShowKeyboardFromFrame:beginFrame toFrame:endFrame];
    };
    
    [UIView animateWithDuration:duration delay:0.0f options:(curve << 16 | UIViewAnimationOptionBeginFromCurrentState) animations:animations completion:nil];
}
- (void)willShowKeyboardFromFrame:(CGRect)beginFrame toFrame:(CGRect)toFrame
{
    if (beginFrame.origin.y == [[UIScreen mainScreen] bounds].size.height)
    {
        [self willShowBottom:toFrame.size.height - [PhoneUtil tabbarAdjustHeight]];
        
        if (self.tableView.myVisibility == MyVisibility_Visible) {
            self.send.selected = NO;
            self.tableView.myVisibility = MyVisibility_Gone;
        }
    }
    else if (toFrame.origin.y == [[UIScreen mainScreen] bounds].size.height)
    {
        [self willShowBottom:0];
    }
    else{
        [self willShowBottom:toFrame.size.height + [PhoneUtil tabbarAdjustHeight]];
    }
     [self layoutAnimationWithDuration:0.5];
}
/*
 *  更改边距
 */
- (void)willShowBottom:(CGFloat)bottom
{
    self.myBottom = bottom;
    
}


@end
